//
//  SearchByChooseViewController.m
//  网络FM
//
//  Created by dlios on 15/5/25.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "SearchByChooseViewController.h"

#import <AFNetworking.h>


#import "SearchAnchorDetailModel.h"

#import "ResultForSearchTableViewCell.h"


#import "PlayerViewController.h"

@interface SearchByChooseViewController ()<UISearchBarDelegate>

@end

@implementation SearchByChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [super dealloc];
}


- (void)loadView
{
    [super loadView];
            [self createSearchBar];
           [self createTableView];

   // [self createTableView];
}

 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];

        
   
        
    }
    return self;
}


#pragma mark - 


- (void)createSearchBar
{
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 40, 30)];
    self.search.placeholder = @"请输入主播名或者心情";
    self.search.delegate = self;
    self.search.autocapitalizationType = UITextAutocorrectionTypeYes;
    self.search.showsCancelButton = NO;
    [self.view addSubview:self.search];
    [self.search setImage:[UIImage imageNamed:@"font-search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    [self.search release];

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchBarText = self.search.text;
}


//
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
  
    NSLog(@"进行拼接的字符串是%@",self.search.text);
    [self SearchConnetion];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.search resignFirstResponder];
    
}

//将输入框里输入的数据作为关键字开始搜索(网络请求)




- (void)SearchConnetion
{
    //choiceDetailcell的网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/broadcast_list.json/?v=1&q="];
    NSString *s = [NSString stringWithFormat:@"%@%@",str,self.search.text];
    
    
    s = [s stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [manager GET:s parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *dic  in array) {
            SearchAnchorDetailModel *model = [[SearchAnchorDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
            
        }
    
        [self.table reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

    
}


- (void)createTableView
{
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStyleGrouped];
    
    [self.table setContentSize:CGSizeMake(0,1000)];
    self.table.dataSource = self;
    self.table.rowHeight = 50;
    self.table.delegate = self;
    
    [self.view addSubview:self.table];
    [self.table release];
     
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellidentify = @"cell";
    ResultForSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentify];
    if (!cell) {
        cell = [[ResultForSearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentify];
    }
    
    SearchAnchorDetailModel *model = [self.array objectAtIndex:indexPath.row];
    cell.title.text =  model.title;
    [cell.title setTintColor:[UIColor purpleColor]];
    return cell;
    

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *p = [[PlayerViewController alloc]init];
    SearchAnchorDetailModel *m = [self.array objectAtIndex:indexPath.row];
    
    p.urlForPlay = m.url;
    [self presentViewController:p animated:YES completion:nil];
}






@end
